package com.example.selectdorm.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.selectdorm.entity.DormitoryBuilding;
import com.example.selectdorm.entity.DormitoryUnit;
import com.example.selectdorm.service.DormitoryBuildingService;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.OutputKeys;
import java.util.List;
import java.util.Optional;


@RestController
@AllArgsConstructor
@RequestMapping("/dorm")
public class DormitoryController {
    private final DormitoryBuildingService dormitoryBuildingService;

    @GetMapping("/findall")
    public ResponseEntity<List<DormitoryBuilding>> getAllDorms(HttpServletRequest request) {
        List<DormitoryBuilding> buildingList = dormitoryBuildingService.findAll();
        return new ResponseEntity<>(buildingList, HttpStatus.OK);
    }

    @GetMapping("/remain")
    public ResponseEntity<String> getRemainBeds(@RequestParam String buildingName) {
        String bedsCount = dormitoryBuildingService.getBedsByName(buildingName);
        return new ResponseEntity<>(bedsCount, HttpStatus.OK);
    }

    @GetMapping("/id")
    public ResponseEntity getID(@RequestParam String buildingName) {
        Optional<DormitoryBuilding> building = dormitoryBuildingService.findBuildingByName(buildingName);
        return new ResponseEntity<>(building, HttpStatus.OK);
    }
}
